//
//  BirthdayAlertTimeModel.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2019/6/11.
//  Copyright © 2019 筱鹏. All rights reserved.
//

import Foundation


fileprivate let BirthdayAlertOffsetTimeKey = "BirthdayAlertOffsetTimeKey"

struct BirthdayAlertOffsetTimeModel {
    
    enum OffsetTime: TimeInterval {
        case today              = 0
        case todayEight         = 28800     // 8*60*60
        case oneDayAgo          = -86400    // - 24*60*60
        case oneDayAgoEight     = -57600    // - 16*60*60
        case twoDayAgo          = -172800   // - 2*24*60*60
        case twoDayAgoEight     = -144000   // - 40*60*60

        
        static var allValue: [OffsetTime] {
            return [.today, .todayEight, .oneDayAgo,
                    .oneDayAgoEight, .twoDayAgo, .twoDayAgoEight]
        }
    }
    
    var currentOffsetTime: OffsetTime {
        let offsetObj = UserDefaults.standard.object(forKey: BirthdayAlertOffsetTimeKey)
        guard let offset = (offsetObj as? TimeInterval) else {
            return .todayEight
        }
        guard let offsetEnum = OffsetTime(rawValue: offset) else {
            return .todayEight
        }
        return offsetEnum
    }
    
    func selectedOffset(_ selected: OffsetTime) {
        UserDefaults.standard.set(selected.rawValue, forKey: BirthdayAlertOffsetTimeKey)
        UserDefaults.standard.synchronize()
    }
    
    func obtainOffsetList() -> [OffsetTime] {
        return OffsetTime.allValue
    }
    
    func obtain(name item:OffsetTime) -> String {
        switch item {
        case .today:
            return "当天0点提醒"
        case .todayEight:
            return "当天8点提醒"
        case .oneDayAgo:
            return "提前一天0点提醒"
        case .oneDayAgoEight:
            return "提前一天8点提醒"
        case .twoDayAgo:
            return "提前两天0点提醒"
        case .twoDayAgoEight:
            return "提前两天8点提醒"
        }
    }
    
}
